package com.sunshine.lnuplus.ui.activity

import android.content.Context
import android.graphics.BitmapFactory
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.callback.TimetableSettingCallBack
import com.sunshine.lnuplus.model.LessonBean
import com.sunshine.lnuplus.model.WidgetData
import com.sunshine.lnuplus.utils.BackgroundUtils
import com.sunshine.lnuplus.utils.BlurBuilder
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.utils.UpdateWidgetUtils
import com.sunshine.lnuplus.view.WidgetSettingView
import com.sunshine.lnuplus.viewModel.SplashViewModel
import kotlinx.android.synthetic.main.activity_timetable_setting.*

class WidgetSettingActivity : BaseActivity() {

    private lateinit var lessonList: ArrayList<LessonBean>
    private lateinit var viewModel: SplashViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_timetable_setting)

        viewModel = ViewModelProvider(this).get(SplashViewModel::class.java)
        StatusBarUtils.setStatusBarWithColor(this, color)
        initView()
        initTheme()
    }

    private fun initTestTimetable() {
        lessonList = ArrayList<LessonBean>().apply {
            add(LessonBean("1", "测试课程1", 1, 1, 1, "教室", "老师", "0", arrayListOf(1)))
            add(LessonBean("2", "测试课程2", 2, 1, 1, "教室", "老师", "0", arrayListOf(2)))
            add(LessonBean("3", "测试课程3", 3, 1, 2, "教室", "老师", "0", arrayListOf(1)))
            add(LessonBean("4", "测试课程4", 4, 1, 2, "教室", "老师", "0", arrayListOf(2)))
            add(LessonBean("5", "测试课程5", 5, 1, 2, "教室", "老师", "0", arrayListOf(1)))
            add(LessonBean("6", "测试课程6", 6, 1, 1, "教室", "老师", "0", arrayListOf(2)))
            add(LessonBean("7", "测试课程7", 7, 1, 1, "教室", "老师", "0", arrayListOf(1)))
        }
        WidgetData.initData(this)
        if(getTimetableTheme() == 1){
            if(getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
                WidgetData.widgetStyle!!.dateAndTimeTextColor = "#ffffff"
            }else{
                WidgetData.widgetStyle!!.dateAndTimeTextColor = "#000000"
            }
        }
        setting_timetable_view.init(
            lessonList = lessonList,
            lessonStyle = WidgetData.widgetStyle!!,
            thisWeek = 1,
            otherWeek = 1,
            fragmentManager = supportFragmentManager,
            isSetting = true,
            fragment = null,
            adapter = null
        )

    }

    private fun initView() {
        initTestTimetable()
        title = "小部件设置"
        val widgetSettingView = WidgetSettingView()
        widgetSettingView.init(object : TimetableSettingCallBack{
            override fun onChanged() {
                setting_timetable_view.init(
                    lessonList = lessonList,
                    lessonStyle = WidgetData.widgetStyle!!,
                    thisWeek = 1,
                    otherWeek = 1,
                    fragmentManager = supportFragmentManager,
                    isSetting = true,
                    fragment = null,
                    adapter = null
                )
            }
        })
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.timetable_setting_view, widgetSettingView)
            .commit()
    }

    private fun initTheme() {
        when(getTimetableTheme()){
            1 -> {
                if(getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
                    setting_background.setImageDrawable(resources.getDrawable(R.color.primary_black))
                }else{
                    setting_background.setImageDrawable(resources.getDrawable(R.color.white))
                }
            }
            2 -> {
                val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.background)
                setting_background.setImageBitmap(
                    BlurBuilder.blur(
                        this,
                        bitmap,
                        (viewModel.getBackgroundMohu()/100.0*25).toFloat()
                    ))
            }
            3 -> {
                BackgroundUtils.setBackground(
                    this,
                    setting_background,
                    true,
                    viewModel.getBackgroundMohu()
                )
            }
            4 -> {
                BackgroundUtils.setUserBackground(
                    this,
                    setting_background,
                    viewModel.getBackgroundMohu()
                )
            }
        }
    }

    override fun onPause() {
        super.onPause()
        UpdateWidgetUtils.updateWidgets(this)
    }

}
